# __init__.py 为初始化加载文件
import time

# 导入系统资源模块
from ascript.android.system import R
# 导入动作模块
from ascript.android import action
# 导入节点检索模块
from ascript.android import node
# 导入图色检索模块
from ascript.android import screen
from ascript.android.ui import Loger




print("Hello AS!")

#========登录
def login():
    try:
        all_node =  node.Selector(1).path("/FrameLayout/LinearLayout/FrameLayout/FrameLayout/FrameLayout/ViewGroup/FrameLayout/LinearLayout/WebView/WebView/View/View/View/View/View/View/EditText").find_all()
        all_node[0].input("test1")
        time.sleep(1)
        all_node[1].input("123456")
        time.sleep(1)
        node.Selector(3).path("/FrameLayout/LinearLayout/FrameLayout/FrameLayout/FrameLayout/ViewGroup/FrameLayout/LinearLayout/WebView/WebView/View/View/View/View").text("输入密码").brother().clickable(True).click().find()
        time.sleep(1)
    except Exception as e:
        print(e)
def watch_ad():

    share_open_pos=screen.FindImages.find_template([R.img("share.png"),],confidence= 0.95)
    if share_open_pos:
        action.click(share_open_pos["result"][0],share_open_pos["result"][1])
        print("=================open share")
    time.sleep(2)
    open_ad_pos = screen.FindImages.find_template([R.img("open_ad.png"),],confidence= 0.95)
    if open_ad_pos:
        action.click(open_ad_pos["result"][0],open_ad_pos["result"][1])
        print("=================open ad")
    for i in range(8):
        print ("================= i",i)
        refuse_pos = screen.FindImages.find_template([R.img("refuse.png"), ], confidence=0.95)
        if refuse_pos:
            print(refuse_pos)
            action.click(refuse_pos["result"][0],refuse_pos["result"][1])
            print("================refuse app")
        time.sleep(5)
    # ad_close_pos = screen.FindColors.find("1001,201,#F6F6F6|1011,190,#FAFAFB|989,189,#FAFAFB|990,211,#F5F3F4|1010,210,#F2F1F1|1007,194,#F8F8F9|996,196,#F5F5F5|996,204,#F0EEEE|1004,204,#F9F9FA")
    # action.click(ad_close_pos)
    # print("================close ad")
    # time.sleep(2)
    # refuse_pos = screen.FindImages.find_template([R.img("refuse.png"), ], confidence=0.95)
    # if refuse_pos:
    #     print(refuse_pos)
    #     action.click(refuse_pos["result"][0], refuse_pos["result"][1])
    #     time.sleep(1)
    #     action.Key.back()
    action.Key.back()
    time.sleep(3)
    share_close_pos = screen.FindImages.find_template([R.img("close_share.png"),],confidence= 0.95)
    if share_close_pos:
        action.click(share_close_pos["result"][0], share_close_pos["result"][1])
        print("=================close share")
    time.sleep(3)
    fresh_coin_pos = screen.FindImages.find_template([R.img("fresh_coin.png"),],confidence= 0.95)
    if fresh_coin_pos:
        action.click(fresh_coin_pos["result"][0], fresh_coin_pos["result"][1])
    time.sleep(2)

    fresh_coin_close = screen.FindImages.find_template([R.img("fresh_coin_close.png"),],confidence= 0.95)
    if fresh_coin_close:
        action.click(fresh_coin_close["result"][0], fresh_coin_close["result"][1])
    print("=================fresh_coin")

def start_watch_ad():
    try:
        watch_ad()
    except Exception as e:
        print("================",e)
        share_open_pos = screen.FindImages.find_template([R.img("share.png"), ], confidence=0.95)
        if not share_open_pos:
            action.Key.back()
            start_watch_ad()